package com.iflytek.runner;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.iflytek.dao.*;
import com.iflytek.document.*;
import com.iflytek.model.*;
import com.iflytek.service.*;
import com.iflytek.utils.CopyUtil;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class LoadCommandLineRunner implements CommandLineRunner {


    @Resource
    private GoodsService goodsService;
    @Resource
    private GoodsDao goodsDao;
    @Resource
    private UserService userService;
    @Resource
    private UserDao userDao;
    @Resource
    private RoleService roleService;
    @Resource
    private RoleDao roleDao;
    @Resource
    private PermissionService permissionService;
    @Resource
    private PermissionDao permissionDao;
    @Resource
    private CartService cartService;
    @Resource
    private CartDao cartDao;
    @Resource
    private OrderService orderService;
    @Resource
    private OrderDao orderDao;

    @Override
    public void run(String... args) throws Exception {
        /**
         * 商品预热
         * 查询goods
         * 复制到es
         */
        List<Goods> goods = goodsService.list();
        List<GoodsDocument> goodsDocuments = CopyUtil.copyList(goods, GoodsDocument.class);
        goodsDao.saveAll(goodsDocuments);
        /**
         * 用户预热
         * 查询user,
         * 复制到es
         */
        List<User> users = userService.list();
        //List<Long> longs = users.stream().map(s -> Long.parseLong(s.getRole())).collect(Collectors.toList());
        List<UserDocument> userDocuments = CopyUtil.copyList(users, UserDocument.class);
        userDao.saveAll(userDocuments);
        System.out.println(userDocuments);
        /**
         * 角色预热
         * 查询role
         * 复制到es
         */
        List<Role> roles = roleService.list();
        List<RoleDocument> roleDocuments = CopyUtil.copyList(roles, RoleDocument.class);
        roleDao.saveAll(roleDocuments);
        /**
         * 权限预热
         * 查询permission
         * 复制到es
         */
        List<Permission> permissions = permissionService.list();
        List<PermissionDocument> permissionDocuments = CopyUtil.copyList(permissions, PermissionDocument.class);
        permissionDao.saveAll(permissionDocuments);

        /**
         * 订单预热
         * 查询order
         * 复制到es
         */
        List<Order> orders = orderService.list();
        List<OrderDocument> orderDocuments = CopyUtil.copyList(orders, OrderDocument.class);
        orderDao.saveAll(orderDocuments);

        /**
         *
         */
        List<Cart> carts = cartService.list();
        List<CartDocument> cartDocuments = CopyUtil.copyList(carts, CartDocument.class);
        if (!CollectionUtils.isEmpty(cartDocuments)){
            cartDao.saveAll(cartDocuments);
        }
        return;
    }

}
  /*  @Override
    public void run(String... args) throws Exception {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        List<Goods> list = goodsService.list(queryWrapper);
        if (!CollectionUtils.isEmpty(list)) {
            List<GoodsDocument> goodsDocuments = CopyUtil.copyList(list, GoodsDocument.class);
            Iterable<GoodsDocument> goodsDocuments1 = goodsDao.saveAll(goodsDocuments);
            goodsDocuments1.forEach(s -> {
            });
        }
    }
        List<OrderItem> orderItems = orderItemService.list();
        List<OrderItemDocument> orderItemDocuments = CopyUtil.copyList(orderItems,OrderItemDocument.class);
        orderItemDao.saveAll(orderItemDocuments);
*/


